﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PingPongGame
{

    public class Ball : MovingObject
    {
        public new const string CollisionGroupString = "ball";

        public int life { get; protected set; }
        public int score { get; protected set; }

        public Ball(Position topLeft, Position speed)
            : base(topLeft, new char[,] { { 'o' } }, speed)
        {
            this.life = 3;
            this.score = 0;
        }


        public override bool CanCollideWith(string otherCollisionGroupString)
        {
            return otherCollisionGroupString == "userRacket" || otherCollisionGroupString == "block";
        }

        public override string GetCollisionGroupString()
        {
            return Ball.CollisionGroupString;
        }

        public override void RespondToCollision(CollisionData collisionData)
        {
            if (collisionData.CollisionForceDirection.Row * this.Speed.Row < 0)
            {
                this.Speed.Row *= -1;
            }
            if (collisionData.CollisionForceDirection.Col * this.Speed.Col < 0)
            {
                this.Speed.Col *= -1;
            }
            if (collisionData.hitObjectsCollisionGroupStrings.Contains("floorBlock"))
            {

                this.TopLeft = new Position(5, 23);
                this.life--;
                if (life == 2) Console.ForegroundColor = ConsoleColor.Yellow;
                else if (life == 1) Console.ForegroundColor = ConsoleColor.Red;
                else
                {
                    Console.WriteLine("The game is over!");
                    Console.WriteLine("Your score is: {0}", this.score);
                    Console.WriteLine("Press [enter] to exit:");
                    Console.ReadLine();
                    Environment.Exit(0);
                   
                }

            }
            if (collisionData.hitObjectsCollisionGroupStrings.Contains("userRacket"))
            {

                this.score += 10;
                if (this.score % 100 == 0)
                {
                    score *= 2;
                }

            }
            if (collisionData.hitObjectsCollisionGroupStrings.Contains("rightBlock"))
            {

                this.score += 5;
                if (this.score % 100 == 0)
                {
                    score *= 2;
                }

            }
            if (collisionData.hitObjectsCollisionGroupStrings.Contains("leftBlock"))
            {

                this.score -= 5;
                if (this.score < 0)
                {
                   score = 0;
                }

            }

        }


    }
}


